/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.dao;

import br.ufc.quixada.adrs.dao.config.PostGresMapConfig;
import br.ufc.quixada.adrs.model.Contato;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Caio
 */
public class ContatoDAO {

    public void insert(Contato contato) throws SQLException {
        Long id = getProxId();
        contato.setId(id);
        PostGresMapConfig.getSqlMapClient().insert("addContato", contato);
    }

    public void update(Contato contato) throws SQLException {
        PostGresMapConfig.getSqlMapClient().update("updateContato", contato);
    }

    public void delete(Long id) throws SQLException {
        PostGresMapConfig.getSqlMapClient().delete("deleteContato", id);
    }

    public Contato getById(Long id) throws SQLException {
        return (Contato) PostGresMapConfig.getSqlMapClient().queryForObject("getContatoById", id);
    }

    public List<Contato> getByUsuarioId(Long id) throws SQLException {
        return (List<Contato>) PostGresMapConfig.getSqlMapClient().queryForList("getContatosByUsuarioId", id);
    }

    private Long getProxId() throws SQLException {
        Long id = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxIdContato");
        if (id == null) {
            id = 0L;
        }
        return id + 1L;
    }
}
